'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AdministratorAdminZjdpCtrl
 * @description
 * # AdministratorAdminZjdpCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
  .controller('AdministratorAdminZjdpCtrl', function ($scope, openApi, Util, $location, $routeParams, FormSubmit) {
    var id = $scope.process_id = $routeParams.id
    $scope.chooseinfo = function () {
      var relModuleObj = {
        name: "AdministratorAdminZjdpCtrl",
        selectTab: "bid_apply",
        selectTabContent: "投标申请单"
      };
      Util.savaParamForReview($scope.param, id);
      Util.savaDataforsearch(relModuleObj);

      $location.path("finance/choosepeople")
    };
    $scope.choosePeople = function () {
      var relModuleObj = {
        name: "AdministratorAdminZjdpCtrl",
        selectTab: "select_apply_userName_by_shift",
        selectTabContent: "持证件"
      };
      Util.savaParamForReview($scope.param, id);
      Util.savaDataforsearch(relModuleObj);

      $location.path("finance/choosepeople")
    };
    //获取证件信息列表
    $scope.getChoosecertId = function () {
      if (!$scope.param.applyUserId) {
        Util.Toast("请先选择持证人")
        return
      }
      var relModuleObj = {
        param: {
          userId: $scope.param.applyUserId
        },
        selectTabContent: "证件"
      };
      Util.savaDataforsearch(relModuleObj);
      Util.saveSelectLists($scope.param.certificates);
      Util.savaParamForReview($scope.param, id);
      $location.path("manage/chooseinfo/zjdp");
    };
    $scope.swipe = function (index) {
      $scope.index = index;
    }
    $scope.resetlist = function (index) {
      $scope.index = -1;
    }

    // 删除证件信息列表
    $scope.deletedItem = function (index) {
      $scope.param.certificates.splice(index, 1);
      $scope.index = -1;
      Util.saveSelectLists($scope.param.certificates);
    };
    var getAllcerIds = function () {
      var cerIds = [],
        cerIdsStr;
      $scope.param.certificates.forEach(function (item, index) {
        cerIds.push(item.cerId);
      });
      cerIdsStr = cerIds.join(",");
      return cerIdsStr;
    }
    $scope.submit = function (operationId) {
      $scope.param.cerIds = getAllcerIds();
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.addcertificateShift;
      options.param = angular.copy($scope.param, {});
      delete options.param.certificates
      delete options.param.formBidApply
      FormSubmit.submit(options,$scope);
    };
    $scope.validate = function () {
      if (!$scope.param.startUseDate) {
        Util.Toast("请选择调证开始时间");
        return;
      }
      if (!$scope.param.endUseDate) {
        Util.Toast("请选择调证结束时间");
        return;
      }
      if (!$scope.param.applyUserName) {
        Util.Toast("请选择持证人");
        return;
      }
      if ($scope.param.certificates.length < 1) {
        Util.Toast("请添加调配证件");
        return;
      }
      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    }
    var init = function () {
      // 获取回显的详细信息
      Util.getDetailData(id, function (data) {
        $scope.param = data;
        // 获取选择页面返回的数据并且合并到参数中
        var selectLists = Util.getSelectLists();
        // 获取选择页面返回的数据并且合并到参数中
        $scope.param.certificates = selectLists || $scope.param.certificates || [];

      });
    };
    // 初始化
    init();
  });
